var talkPageNum = 1;
var signedPageNum = 1;
var itemNum = 20;
var code = 100;
var isDown = false;
var isUp = false;
var keyStr = "";
var height;
(function($) {

	mui.init({
		keyEventBind: {
			backbutton: true //开启back按键监听
		}
	});
	setPxPerRem();
	mui.plusReady(function() {
		var clientWidth = getQueryString("clientWidth");
		height = getQueryString("height");
		keyStr = getQueryString("keyword");
		var sliderSegmentedControl = document.getElementById("sliderSegmentedControl");
		item1.style.minHeight = (height - sliderSegmentedControl.clientHeight - 2) + "px";
		item2.style.minHeight = (height - sliderSegmentedControl.clientHeight - 2) + "px";
		setPxPerRem(clientWidth);

		if(!isEmpty(keyStr)) {
			mui.back = function(event) {
				plus.navigator.setStatusBarBackground('#FFFFFF');
				var ws = plus.webview.currentWebview();
				plus.webview.close(ws);
				return false;
			};
		}
		if(isHaveNet()) {
			getProjectList(2, document.getElementById("talkScroll"));
		} else {
			mui.toast("网络连接失败，请连接网络后再试")
		}

	});
	$('.mui-scroll-wrapper').scroll({
		indicators: true //是否显示滚动条
	});

	$.ready(function() {
		//		$.each(document.querySelectorAll('.mui-slider-group .mui-scroll'), function(index, pullRefreshEl) {
		//			$(pullRefreshEl).pullToRefresh({
		//				down: {
		//					callback: function() {
		//						var self = this;
		//						var pageNum = 1;
		//						if(index == 0) {
		//							code = 0;
		//							removeLi("talkList");
		//							talkPageNum = 1;
		//						} else {
		//							code = 1;
		//							removeLi("signedList");
		//							signedPageNum = 1;
		//						}
		//						plus.nativeUI.showWaiting();
		//						isDown = true;
		//						getProjectList(code, self);
		//					}
		//				},
		//				up: {
		//					callback: function() {
		//
		//						var num;
		//						var self = this;
		//						if(index == 0) {
		//							code = 0;
		//							num = talkPageNum;
		//						} else {
		//							code = 1;
		//							num = signedPageNum;
		//						}
		//						plus.nativeUI.showWaiting();
		//						isUp = true;
		//						getProjectList(code, self)
		//					}
		//				}
		//			});
		//		});
	})

})(mui);

var item2 = document.getElementById('item2mobile');
var item1 = document.getElementById('item1mobile');
var html2 = '';
document.getElementById('slider').addEventListener('slide', function(e) {
	if(e.detail.slideNumber === 1) {
		if(item2.querySelector('.mui-loading')) {}
	}
});
var sliderGroup = document.getElementById("sliderGroup");
var talkDiv = document.getElementById("talkDiv");
var signedDiv = document.getElementById("signedDiv");

function getProjectList(tabTag, ele) {
	var token = localStorage.getItem("token_type") + " " + localStorage.getItem("access_token");
	if(isEmpty(token)) {
		mui.toast("请重新登陆！");
	}
	var opts = {
		url: '/user/zs_project/category',
		addressUrl: "testline",
		requestType: "GET",
		authorization: token
	}

	crossDomainAjax(opts, function(data) {
		plus.nativeUI.closeWaiting();
		console.log(JSON.stringify(data));
		if(!isEmpty(data)) {
			var rows = data.data;
			if(!isEmpty(rows) && rows.length == 2) {

				for(var i = 0; i < rows.length; i++) {
					if(tabTag == i) {
						var insertDiv = signedDiv;
						if(tabTag == 1) {
							insertDiv = talkDiv;
						}
						createProjectItem(rows[i], insertDiv);
					} else {
						if(i == 0) {
							createProjectItem(rows[i], signedDiv);
						} else {
							createProjectItem(rows[i], talkDiv);
						}
					}
				}

			}
		} else {
			mui.toast("数据请求失败");
		}

		//		if(isDown) {
		//			ele.endPullDownToRefresh();
		//		}
		//		if(isUp) {
		//			ele.endPullUpToRefresh();
		//		}
	});
}

function createProjectItem(row, insertDiv, tag) {

	if(isEmpty(tag)) {
		tag = false;
	}

	if(!isEmpty(row)) {
		var items = row.childs;
		for(var i = 0; i < items.length; i++) {
			var id = items[i].id;
			var name = items[i].name;
			var description = items[i].description;
			var desStr = '<p class="poilcy-time">' + description + '</p>'
			if(isEmpty(description)) {
				desStr = "";
			}
			var createLi = document.createElement('li');
			createLi.id = id;
			createLi.className = "mui-table-view-cell";
			createLi.innerHTML = '<p class="policy-title">' + name + '</p>' + desStr;
			document.body.appendChild(createLi);
			insertBefore(createLi, insertDiv);
		}
	}
}
mui("#sliderGroup").on('tap', "li", function() {
	var titleText = this.getElementsByTagName("p")[0].innerHTML;
	console.log(titleText)
	var postData = {
		pageflag: "page_project.html",
		titleStr: titleText,
		itemId: this.id
	}
	var change = {
		pageUrl: "page_projectinfo.html",
		postData: postData,
		aniShow: "fade-in"
	}
	pageChange(change);

});